/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.agileClinic.managedBean;

import br.com.agileClinic.controller.RacaControl;
import br.com.agileClinic.modelo.Raca;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.persistence.PersistenceException;

/**
 * Bean gerenciado do Projeto guardiao criado em 02/04/2013
 *
 * @author ari
 */
@ManagedBean

public class RacaMb implements Serializable {

    @Inject
    private RacaControl control;
    
    private Raca racaSelecionada;
    private List<Raca> listaRacas = new ArrayList<Raca>();

    public RacaMb() {
        racaSelecionada = new Raca();
    }

    public void salvar() {
        try {
            control.salvarouAtualizar(racaSelecionada);
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO, "Registro de Raça incluido com sucesso", ""));
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (PersistenceException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (EJBException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        }
    }

    public void listarTodos() {
        try {
            
            listaRacas = control.listarTodos();
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (PersistenceException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (EJBException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        }
       // return listaRacas;
       
    }
    
    public void deletar(){
        try {
            control.deletar(racaSelecionada);
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (PersistenceException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (EJBException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, UltilMb.getMsgErro(), ""));
        }
    }
    
   

    public Raca getRacaSelecionada() {
        return racaSelecionada;
    }

    public void setRacaSelecionada(Raca racaSelecionada) {
        this.racaSelecionada = racaSelecionada;
    }

    public List<Raca> getListaRacas() {
        return listaRacas;
    }

    public void setListaRacas(List<Raca> listaRacas) {
        this.listaRacas = listaRacas;
    }
    
    
}
